package com.searchparking.BUS;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolylineOptions;

import android.content.Context;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.util.Log;
import android.widget.Toast;

/*
 * Dung de lay thong tin tu goolge tra ve thong tin dia diem hien tai
 */
public class ParkingInfo {
	protected String longitude;
	protected String latitude;
	protected String name;
	protected PolylineOptions lineOptions;
	protected ArrayList<LatLng> points;
	protected String distance ;
	protected String duration ;
	public String strUrl;
	public ParkingInfo()
	{
		lineOptions=null;
		distance="";
		duration="";
		points = null;
	}
	/*
	 * Lay du lieu tu url
	 */
	public String getDataUrl() throws IOException
	{
		String data= null;
		InputStream iStream = null;
		HttpURLConnection urlConnection = null;
		
		try {
			URL url = new URL(strUrl);

			// Creating an http connection to communicate with url
			urlConnection = (HttpURLConnection) url.openConnection();

			// Connecting to url
			urlConnection.connect();

			// Reading data from url
			iStream = urlConnection.getInputStream();

			BufferedReader br = new BufferedReader(new InputStreamReader(
					iStream));

			StringBuffer sb = new StringBuffer();

			String line = "";
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}

			data = sb.toString();

			br.close();

		} catch (Exception e) {
			Log.d("Exception while downloading url", e.toString());
		} finally {
			//iStream.close();
			urlConnection.disconnect();
		}
		return data;
		
	}
	/*
	 * Chuyen thong tin chuoi json 
	 */
	public void setInfoParking(String jsonData)
	{
		JSONObject jObject;
		DirectionsJSONParser parser;
		List<List<HashMap<String, String>>> result = null;

		try {
			jObject = new JSONObject(jsonData);
			parser = new DirectionsJSONParser();

			// Starts parsing data
			result = parser.parse(jObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		// list points
		
		// pipeline to draw
		

		

		// Traversing through all the routes
		for (int i = 0; i < result.size(); i++) {
			points = new ArrayList<LatLng>();
			lineOptions = new PolylineOptions();

			// Fetching i-th route
			List<HashMap<String, String>> path = result.get(i);

			// Fetching all the points in i-th route
			for (int j = 0; j < path.size(); j++) {
				HashMap<String, String> point = path.get(j);

				if (j == 0) { // Get distance from the list
					distance = point.get("distance");
					continue; //go to 'for statement'
				} else if (j == 1) { // Get duration from the list
					duration = point.get("duration");
					continue; //go to 'for statement'
				}

				double lat = Double.parseDouble(point.get("lat"));
				double lng = Double.parseDouble(point.get("lng"));

				LatLng position = new LatLng(lat, lng);
				points.add(position);
			}

			// Adding all the points in the route to LineOptions
			lineOptions.addAll(points);
			// set width line to draw
			lineOptions.width(5);
			// set color
			lineOptions.color(Color.RED);
		}

		Log.d("DISTANCE","Distance:" + distance + ", Duration:"
				+ duration);
	}
	public void getDirectionsUrl(LatLng origin, LatLng dest) {
		// Origin of route
        String str_origin = "origin=" + origin.latitude + ","
                + origin.longitude;

        // Destination of route
        String str_dest = "destination=" + dest.latitude + "," + dest.longitude;

        // Sensor enabled
        String sensor = "sensor=false";

        // Building the parameters to the web service
        String parameters = str_origin + "&" + str_dest + "&" + sensor;

        // Output format
        String output = "json";

        // Building the url to the web service
        this.strUrl = "https://maps.googleapis.com/maps/api/directions/"
                + output + "?" + parameters;
         Log.d("URL",strUrl);

        
    }
	public String getDistance(String jsonData)
	{
		setInfoParking(jsonData);
		return  this.distance;
	}
	public String getDuaration() {
		// TODO Auto-generated method stub
		return this.duration;
	}

	
	
	
}
